package com.situ.mall.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.mall.common.ServerResponse;
import com.situ.mall.entity.Category;
import com.situ.mall.entity.Product;
import com.situ.mall.mapper.CategoryMapper;
import com.situ.mall.mapper.ProductMapper;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.service.IProductService;

@Service
public class CategoryServiceImpl implements ICategoryService {
	@Autowired
	private CategoryMapper categoryMapper;

	@Override
	public List<Category> list() {
		return categoryMapper.list();
	}

	@Override
	public ServerResponse pageList(Integer page, Integer limit) {
		// 1、使用PageHelper插件设置分页
		PageHelper.startPage(page, limit);
		// 2、执行查询
		List<Category> list = categoryMapper.pageList();
		// 3、使用PageInfo对结果进行包装
		PageInfo pageInfo = new PageInfo(list);
		Integer count = (int) pageInfo.getTotal();// 得到总数量

		return ServerResponse.createSuccess("查询成功", count, list);
	}

	@Override
	public ServerResponse deleteById(Integer id) {
		try {
			int count = categoryMapper.deleteByPrimaryKey(id);
			if (count == 1) {
				return ServerResponse.createSuccess("删除成功");
			} else {
				return ServerResponse.createSuccess("删除失败");
			}

		} catch (Exception e) {
			return ServerResponse.createSuccess("删除失败");
		}
	}

	@Override
	public ServerResponse deleteAll(String ids) {
		String[] idArray = ids.split(",");
		int count = categoryMapper.deleteAll(idArray);
		if (count >= 1) {
			return ServerResponse.createSuccess("删除成功");
		} else {
			return ServerResponse.createSuccess("删除失败");
		}
	}
	@Override
	public ServerResponse add(Category category) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ServerResponse selectTopCategory() {
		List<Category> list = categoryMapper.selectTopCategory();
		System.out.println("一级联动list="+list);
		return ServerResponse.createSuccess("查找成功", list);
	}

	@Override
	public ServerResponse selectSecondCategory(Integer topCategoryId) {
		List<Category> list = categoryMapper.selectSecondCategory(topCategoryId);
		System.out.println("二级联动list="+list);
		return ServerResponse.createSuccess("查找成功", list);
	}

	@Override
	public List<Category> selectTopCategoryList() {
		return categoryMapper.selectTopCategory();
	}

	@Override
	public List<Category> selectSecondCategoryList() {
		return categoryMapper.selectSecondCategoryList();
	}
}
